//
//  ByLARTypeSelectView.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/18.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByLARTypeSelectView.h"

@implementation ByLARTypeSelectView
{
    NSMutableArray *widthsAry;              // 宽度数组
    
    NSInteger prevTag;                      // 上个选中的tag
}

- (void)setTypesAry:(NSArray *)typesAry {
    _typesAry = typesAry;
    
    CGFloat selfWidth = 0;
    /** 计算宽度 */
    widthsAry = [[NSMutableArray alloc] initWithCapacity:_typesAry.count];
    for (NSString *type in _typesAry) {
        CGFloat width = [type getSizeOfString:12].width + 12 + 5;
        [widthsAry addObject:@(width)];
        
        selfWidth += width;
    }
    
    self.width = selfWidth + 20;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    prevTag = 0;
    
    self.selectType = 0;
    
    /** 循环创建视图 */
    CGFloat x = 0, y, width, height;
    CGFloat xOffset = 0;
    for (int i = 0; i < self.typesAry.count; i ++) {
        x = xOffset;
        y = 0;
        width = [[widthsAry objectAtIndex:i] floatValue];
        height = self.height;
        UIView *basalView = [[UIView alloc] init];
        basalView.frame = CGRectMake(x, y, width, height);
        [self addSubview:basalView];
        basalView.tag = 10 + i;         // 用于取出
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAction:)];
        [basalView addGestureRecognizer:tap];
        
        /** 选中图片 */
        x = 0;
        y = 0;
        width = 12;
        height = 12;
        UIImageView *selectView = [[UIImageView alloc] init];
        selectView.userInteractionEnabled = YES;
        selectView.frame = CGRectMake(x, y, width, height);
        selectView.image = [UIImage imageNamed:@"选择-未确定"];
        selectView.tag = 1;
        [basalView addSubview:selectView];
        selectView.center = CGPointMake(selectView.center.x, basalView.height / 2);

        
        /** 文字 */
        x = selectView.right + 5;
        y = 0;
        width = basalView.width - 12 - 5;
        height = basalView.height;
        UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        typeLabel.userInteractionEnabled = YES;
        typeLabel.font = FONT(12);
        typeLabel.text = self.typesAry[i];
        typeLabel.textColor = [UIColor blackColor];
        typeLabel.textAlignment = NSTextAlignmentLeft;
        typeLabel.tag = 2;
        [basalView addSubview:typeLabel];
        
        xOffset += [[widthsAry objectAtIndex:i] floatValue] + 10;

    }
    
    [self changeViewStateWithTag:10 selected:YES];
    prevTag = 10;
    self.selectType = 6;

}

#pragma mark - 点击事件
- (void)clickAction:(UITapGestureRecognizer *)tap {
    
    NSInteger curTag = tap.view.tag;

    if (prevTag == 0) {     // 还未选择
        [self changeViewStateWithTag:curTag selected:YES];
    }
    else {
        if (curTag == prevTag) {
            return;
        }
        
        [self changeViewStateWithTag:curTag selected:YES];
        [self changeViewStateWithTag:prevTag selected:NO];
    }
    
    prevTag = curTag;
    if (curTag == 10) {             // 企业
        self.selectType = 6;
    } else if (curTag == 11) {      // 学生
        self.selectType = 5;
    }
    
}
- (void)changeViewStateWithTag:(NSInteger)tag selected:(BOOL)selected {
    UIView *basalView = [self viewWithTag:tag];
    UIImageView *selectView = (UIImageView *)[basalView viewWithTag:1];
    selectView.image = selected ? [UIImage imageNamed:@"选择-确定"] : [UIImage imageNamed:@"选择-未确定"];
    UILabel *typeLabel = (UILabel *)[basalView viewWithTag:2];
    typeLabel.textColor = selected ? JM_THEME_COLOR : [UIColor blackColor];
}

@end
